package android.slc.module.component.dict.repository;

import android.slc.base.api.slc.callback.po.SlcEntity;
import android.slc.base.api.slc.config.ApiConfig;
import android.slc.base.api.slc.converter.SlcEntityErrorException;
import android.slc.base.vm.GlobalDataVm;

import java.util.List;

import android.slc.box.commonlibrary.compat.SlcResourceUtils;
import android.slc.box.or.SlcNu;
import android.slc.box.or.SlcParam;
import android.slc.module.component.dict.entity.TreeDict;
import android.slc.module.component.dict.entity.TreeDictWrap;
import android.slc.module.component.dict.vmbox.TreeDictShareVm;
import android.slc.rx.SlcRxJavaUtils;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Function;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 就是一个字段获取的例子而已
 *
 * @author slc
 * @date 2020-09-11 11:04
 */
public class DictionaryRepository {
    /**
     * 根据字典组id获取字典
     *
     * @param code
     * @return
     */
    public static Observable<SlcEntity<TreeDictWrap>> dictionary(String code) {
        return dictionary(code, true);
    }

    /**
     * 根据字典组id获取字典
     *
     * @param code
     * @return
     */
    public static Observable<SlcEntity<TreeDictWrap>> dictionary(String code, boolean useCache) {
        TreeDictShareVm treeDictShareVm = GlobalDataVm.getInstance().treeDictShareVm;
        if (!useCache || !treeDictShareVm.dictListIsExist(code)) {
            return SlcNu.getInstance().create(DictionaryService.class)
                    .dictionary(code)
                    .map(new Function<SlcEntity<List<TreeDict>>, SlcEntity<TreeDictWrap>>() {
                        @Override
                        public SlcEntity<TreeDictWrap> apply(@NonNull SlcEntity<List<TreeDict>> listSlcEntity) throws Exception {
                            SlcEntity<TreeDictWrap> sysDictWrapSlcEntity = new SlcEntity<>();
                            sysDictWrapSlcEntity.setCode(listSlcEntity.getCode());
                            sysDictWrapSlcEntity.setMsg(listSlcEntity.getMsg());
                            if (listSlcEntity.isSuccess()) {
                                if (CollectionUtils.isNotEmpty(listSlcEntity.getData())) {
                                    sysDictWrapSlcEntity.setData(treeDictShareVm.putDictList(code, listSlcEntity.getData()));
                                    return sysDictWrapSlcEntity;
                                }
                                throw new SlcEntityErrorException(ApiConfig.Value.SERVER_ERROR, SlcResourceUtils.getString("label_data_is_null"));
                            }
                            return sysDictWrapSlcEntity;
                        }
                    })
                    .compose(SlcRxJavaUtils.applyOoAndroidSchedulers());
        } else {
            return Observable.just(SlcEntity.getSlcEntityByData(treeDictShareVm.getSysDictWrap(code)));
        }
    }

    public static Observable<SlcEntity<TreeDictWrap>> childListOnly(String code) {
        return childListOnly(code, null, true);
    }

    public static Observable<SlcEntity<TreeDictWrap>> childListOnly(String code, boolean useCache) {
        return childListOnly(code, null, useCache);
    }

    public static Observable<SlcEntity<TreeDictWrap>> childListOnly(String code, String parentId) {
        return childListOnly(code, parentId, true);
    }

    public static Observable<SlcEntity<TreeDictWrap>> childListOnly(String code, String parentId, boolean useCache) {
        String codeFinal = code;
        SlcParam.Builder builder = SlcParam.newBuilder();
        builder.put("code", code);
        if (!StringUtils.isEmpty(parentId)) {
            builder.put("parentId", parentId);
            codeFinal = codeFinal + ":p-" + parentId;
        }
        String codeFinalTemp = codeFinal;
        TreeDictShareVm treeDictShareVm = GlobalDataVm.getInstance().treeDictShareVm;
        if (!useCache || !treeDictShareVm.dictListIsExist(codeFinal)) {
            return SlcNu.getInstance().create(DictionaryService.class)
                    .childListOnly(builder.build())
                    .map(new Function<SlcEntity<List<TreeDict>>, SlcEntity<TreeDictWrap>>() {
                        @Override
                        public SlcEntity<TreeDictWrap> apply(@NonNull SlcEntity<List<TreeDict>> listSlcEntity) throws Exception {
                            SlcEntity<TreeDictWrap> sysDictWrapSlcEntity = new SlcEntity<>();
                            sysDictWrapSlcEntity.setCode(listSlcEntity.getCode());
                            sysDictWrapSlcEntity.setMsg(listSlcEntity.getMsg());
                            if (listSlcEntity.isSuccess()) {
                                if (CollectionUtils.isNotEmpty(listSlcEntity.getData())) {
                                    sysDictWrapSlcEntity.setData(treeDictShareVm.putDictList(codeFinalTemp, listSlcEntity.getData()));
                                    return sysDictWrapSlcEntity;
                                }
                                throw new SlcEntityErrorException(ApiConfig.Value.SERVER_ERROR, SlcResourceUtils.getString("label_data_is_null"));
                            }
                            return sysDictWrapSlcEntity;
                        }
                    })
                    .compose(SlcRxJavaUtils.applyOoAndroidSchedulers());
        } else {
            return Observable.just(SlcEntity.getSlcEntityByData(treeDictShareVm.getSysDictWrap(codeFinal)));
        }
    }

}
